﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormExam
{
    public partial class EidtForm : Form
    {
        public EidtForm()
        {
            InitializeComponent();
        }
        public EidtForm(int id,string title,string author)
        {
            InitializeComponent();
            this.id = id;
            txtcombox1.Text = title;
            txtcombox2.Text = author.ToString();
             
        }
        private int id;
        private void button1_Click(object sender, EventArgs e)
        {
            var sql = string.Format("insert into Articles(Title,Author)values('{0}','{1}')", txtcombox1.Text, txtcombox2.Text);
            var res = DbHelper.AddOrEditOrDeleteSave(sql);
            //更新
            if (id > 0)
            {
                if (!string.IsNullOrEmpty(txtcombox1.Text))
                {
                    var sqls = string.Format("update Articles set Title='{0}',Author='{1}' where Id='{2}'",txtcombox1,txtcombox2,this.id);
                    var resq = DbHelper.AddOrEditOrDeleteSave(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("更新成功", "prompt");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                }
            }
            //新增
            else
            {
                 
                if (!string.IsNullOrEmpty(txtcombox1.Text))
                {
                    var sqls = string.Format("insert into Articles(Title,Author)values('{0}','{1}')",txtcombox1.Text,txtcombox2.Text);
                    var resq = DbHelper.AddOrEditOrDeleteSave(sql);
                    if (res == 1)
                    {
                        MessageBox.Show("添加成功", "prompt");
                        this.DialogResult = DialogResult.Yes;
                        this.Close();
                    }
                    //else
                    //{
                    //    MessageBox.Show("添加失败", "prompt");
                    //    this.Close();
                    //}
                }
                else
                {
                    MessageBox.Show("姓名不能为空", "prompt");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No; 
            this.Close();
        }


    }
}
